// 正确导入方式（应统一为）
import { BaseDTO } from '#src/common/dto/base.dto.js';
import Joi from 'joi';

/**
 * @file response/user.js
 * @description 用户相关的响应DTO
 */

/**
 * 用户登录响应DTO
 * @extends BaseDTO
 */
export class UserLoginResponseDTO extends BaseDTO {
    static schema = Joi.object({
        token: Joi.string().required(),
        id: Joi.string().required(),
        username: Joi.string().required(),
        email: Joi.string().email().required(),
    });

    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    // 应该在此处定义转换方法
    static fromServiceResponse({ token, user }) {
        return new UserLoginResponseDTO({
            token: token,
            id: user.id,
            username: user.username,
            email: user.email
        });
    }
}

/**
 * 用户信息响应DTO
 * @extends BaseDTO
 */
export class UserProfileDTO extends BaseDTO {
    /** @type {Joi.Schema} */
    static schema = Joi.object({
        user_id: Joi.string().required(),
        username: Joi.string().required(),
        email: Joi.string().email().required(),
    });

    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    static fromServiceResponse(serviceResponse) {
        return new UserProfileDTO(serviceResponse);
    }
}

/**
 * 用户列表项DTO
 * @extends BaseDTO
 */
export class UserListItemDTO extends BaseDTO {
    /** @type {Joi.Schema} */
    static schema = Joi.object({
        user_id: Joi.string().required(),
        username: Joi.string().required(),
        email: Joi.string().email().required(),
    });

    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    static fromServiceResponse(serviceResponse) {
        return new UserListItemDTO(serviceResponse);
    }
}

/**
 * 用户注册响应DTO
 * @extends BaseDTO
 */
export class UserRegisterResponseDTO extends BaseDTO {
    /** @type {Joi.Schema} */
    static schema = Joi.object({
        userId: Joi.string().required(),
        message: Joi.string().required()
    });

    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
        this.timestamp = new Date().toISOString();
    }

    static fromServiceResponse(serviceResponse) {
        return new UserRegisterResponseDTO(serviceResponse);
    }
}